package wql.boot.wqlspringboot02.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

/**
 * @author wql
 * @version 1.0
 * @date 2022/11/15 17:24
 */

@Component    //该注解把类实例化到spring容器中相当于配置文件
public class MyBrowserRunner implements CommandLineRunner {

    //框架自带的日志 打印信息到控制台
    private static Logger logger = LoggerFactory.getLogger(MyBrowserRunner.class);

    //通过该注解@Value 利用spel表达式(${spring.web.loginurl})获取配置文件的值
    @Value("${spring.web.loginurl}")
    private String loginUrl;

    @Value("${spring.web.googleexcute}")
    private String googleExcutePath;

    @Value("${spring.auto.openurl}")
    private boolean isOpen;

    @Override
    public void run(String... args) throws Exception {
        if(isOpen){
            String cmd = googleExcutePath +" "+ loginUrl;
            Runtime run = Runtime.getRuntime();
            try{
                run.exec(cmd);
                logger.debug("启动浏览器打开项目成功");
            }catch (Exception e){
                e.printStackTrace();
                logger.error(e.getMessage());
            }
        }
    }
}
